#!/usr/bin/env python3
"""
Example usage of image preprocessing features in OCR service
"""

import os
import sys
import base64
import requests

# Add parent directory to path to import modules
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

def example_basic_usage():
    """Example of basic OCR with preprocessing"""
    print("=== Basic OCR with Preprocessing ===")
    
    # If you have an image file, you can use this code:
    # files = {'file': open('your_image.jpg', 'rb')}
    # data = {
    #     'enhance_image': True,
    #     'correct_skew': True
    # }
    # response = requests.post('http://localhost:8001/api/ocr/recognize', files=files, data=data)
    # print("Response:", response.json())
    
    print("To use this example:")
    print("1. Make sure the OCR server is running (python main.py)")
    print("2. Uncomment the code above and replace 'your_image.jpg' with your image file")
    print("3. Run this script")

def example_digit_recognition():
    """Example of digit-only OCR with preprocessing"""
    print("\n=== Digit-Only OCR with Preprocessing ===")
    
    # If you have an image file with digits, you can use this code:
    # files = {'file': open('image_with_digits.jpg', 'rb')}
    # params = {
    #     'enhance_image': True,
    #     'confidence_threshold': 0.8
    # }
    # response = requests.post('http://localhost:8001/api/ocr/digits', files=files, params=params)
    # print("Response:", response.json())
    
    print("To use this example:")
    print("1. Make sure the OCR server is running (python main.py)")
    print("2. Uncomment the code above and replace 'image_with_digits.jpg' with your image file")
    print("3. Run this script")

def example_license_plate_aware():
    """Example of license plate-aware OCR with preprocessing"""
    print("\n=== License Plate-Aware OCR with Preprocessing ===")
    
    # If you have an image file, you can use this code:
    # files = {'file': open('document_with_license_plate.jpg', 'rb')}
    # params = {
    #     'enhance_image': True,
    #     'correct_skew': True,
    #     'remove_duplicates': True
    # }
    # response = requests.post('http://localhost:8001/api/ocr/digits_without_license_plate', files=files, params=params)
    # print("Response:", response.json())
    
    print("To use this example:")
    print("1. Make sure the OCR server is running (python main.py)")
    print("2. Uncomment the code above and replace 'document_with_license_plate.jpg' with your image file")
    print("3. Run this script")

def example_command_line_usage():
    """Example of command-line usage"""
    print("\n=== Command-Line Usage Examples ===")
    print("Basic OCR with enhancement:")
    print("curl -X POST \"http://localhost:8001/api/ocr/recognize?enhance_image=true\" \\")
    print("     -H \"accept: application/json\" \\")
    print("     -H \"Content-Type: multipart/form-data\" \\")
    print("     -F \"file=@document.jpg\"")
    
    print("\nDigit-only OCR with both enhancement and skew correction:")
    print("curl -X POST \"http://localhost:8001/api/ocr/digits?enhance_image=true&correct_skew=true\" \\")
    print("     -H \"accept: application/json\" \\")
    print("     -H \"Content-Type: multipart/form-data\" \\")
    print("     -F \"file=@invoice.jpg\"")
    
    print("\nLicense plate-aware OCR with preprocessing:")
    print("curl -X POST \"http://localhost:8001/api/ocr/digits_without_license_plate?enhance_image=true&correct_skew=true\" \\")
    print("     -H \"accept: application/json\" \\")
    print("     -H \"Content-Type: multipart/form-data\" \\")
    print("     -F \"file=@id_document.jpg\"")

if __name__ == "__main__":
    print("OCR Image Preprocessing Examples")
    print("=" * 40)
    
    example_basic_usage()
    example_digit_recognition()
    example_license_plate_aware()
    example_command_line_usage()
    
    print("\nFor more detailed information, see:")
    print("- IMAGE_PREPROCESSING_DOCUMENTATION.md")
    print("- API_DOCUMENTATION.md")
    print("- DIGITS_OCR_DOCUMENTATION.md")
    print("- LICENSE_PLATE_OCR_DOCUMENTATION.md")